package com.chinasoft.controller;

import com.chinasoft.pojo.Result;
import com.chinasoft.pojo.User;
import com.chinasoft.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/user")
public class LoginController {
    @Autowired
    UserServiceImpl userServiceImpl;

    @PostMapping("/list")
    public List<User>  list() {
        return userServiceImpl.findAll();
    }

    @PostMapping("/login")
    public Result login(@RequestBody User acc){
        Result result = new Result();
        User login = userServiceImpl.login(acc);
        if(login == null){
            return Result.fail(500, "登录失败");
        }else{
            return new Result(login, "添加成功");
        }
    }

    @GetMapping("/search/{teachername}")
    public Result getOne(@PathVariable String teachername) {
        User byId = userServiceImpl.getById(teachername);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }

    @PostMapping("/find/{usertype}")
    public List<User> getType(@PathVariable String usertype) {
       return userServiceImpl.getByType(usertype);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        boolean update = userServiceImpl.update(user.getUsername(), user.getType(), user.getEndtime());
        if (update) {
            return new Result(null, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }

    @DeleteMapping("/delete/{username}")
    public Result delete(@PathVariable String username) {
        boolean delete = userServiceImpl.delete(username);
        if (delete) {
            return new Result(null, "删除成功");
        } else {
            return Result.fail(500, "删除失败");
        }
    }

    @PostMapping("/addTeacher")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody User brand) {
        boolean insertBrand = userServiceImpl.addTeacher(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @PostMapping("/addUser")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertUser(@RequestBody User brand) {
        boolean insertBrand = userServiceImpl.addUser(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @PostMapping("/updateintime")
    public Result updateIntime(@RequestBody User brand) {
        boolean updateBrand = userServiceImpl.updateTeacher(brand.getUsername(), brand.getIntime());
        if (updateBrand) {
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }

    @PostMapping("/setpassword")
    public Result updatePassword(@RequestBody User brand) {
        boolean updateBrand = userServiceImpl.setPassword(brand.getUsername(), brand.getPassword());
        if (updateBrand) {
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
}
